<?php

namespace catchAdmin\tenant\controller;

use catchAdmin\tenant\model\Users;
use catcher\base\CatchRequest as Request;
use catcher\CatchAuth;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\tenant\model\Tenant as TenantModel;
use think\Response;
use think\facade\Db;

class Tenant extends CatchController
{
    
    protected $tenantModel;
    protected $users;

    /**
     *
     * @time 2021/11/18 14:50
     * @param TenantModel $tenantModel
     * @return mixed
     */
    public function __construct(TenantModel $tenantModel,Users $users)
    {
        $this->tenantModel = $tenantModel;
        $this->users = $users;
    }
    
    /**
     *
     * @time 2021/11/18 14:50
     * @return Response
     */
    public function index() : Response
    {
        return CatchResponse::paginate($this->tenantModel->getList());
    }

    /**
     *
     * @time 2021/11/18 14:50
     * @return Response
     */
    public function getEmployee(CatchAuth $auth)
    {
        $user = $auth->user();
        $where = [];
        if($user['tenant_id']){
            $where[] = ['tenant_id','=',$user['tenant_id']];
        }
        $where[] = ['deleted_at','=',0];
        $where[] = ['position','=',2];
        $employee = Db::table('users')->where($where)->select();
        return CatchResponse::success($employee,'查询成功');
    }
    
    /**
     *
     * @time 2021/11/18 14:50
     * @param Request $request
     * @return Response
     */
    public function info(CatchAuth $auth)
    {
        $user = $auth->user();
        $user = $this->users->field('id,username,merchant_name,merchant_id,merchant_secret,ebp_name,ebp_code')->where([['tenant_id','=',$user['tenant_id']],['type','in','1,2']])->find();
        return CatchResponse::success($user,'查询成功');
    }

    /**
     *
     * @time 2021/11/18 14:50
     * @param $id
     * @param Request $request
     * @return Response
     */
    public function update(Request $request)
    {
        $params = $request->param();
        $id = $params['id'];
        $this->users->updateBy($id, $params);
        return CatchResponse::success([],'操作成功');
    }

}